package com.bqtt.tumor.demo.entity;

import java.util.HashMap;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.bqtt.tumor.demo.common.utility.InlineTools;


@Entity
@Table(name="lab_sheet_def") 
public class LabSheet  implements IDBEntity{

	
	@Id 
    @Column(name="sheet_def_Id")    
    @GeneratedValue(strategy=GenerationType.IDENTITY) 
	private int sheet_def_Id;
	
	@Column(name="name", length= 255)   
	private String name;
	
	@Column(name="fields", length= 1000)   
	private String fields;

	@Column(name="unit", length= 1000)   
	private String unit;
	
	@Transient
	private String displayName;
	
	
	@Transient
	private String fields_details;
	
	public String getDisplayName() {
		return displayName;
	}

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}
	
	
	
	public String getFields_details() {
		return fields_details;
	}

	public void setFields_details(String fields_details) {
		this.fields_details = fields_details;
	}

	@Override
	public Map<String, Object> getSQLValidAttributes() {
		// TODO Auto-generated method stub

		Map<String,Object> result = new HashMap<String,Object>();
		
		if(!InlineTools.isEmpty(name))
		{
			result.put("name", name);
		}


		return result;
	}

	//DB to MVC entity
	@Override
	public void fillStringField() {
		// TODO Auto-generated method stub
		String [] fieldArray = fields.split("\\|");
		String [] unitArray = unit.split("\\|");
		if(fieldArray.length == unitArray.length)
		{
			String temp = "";
			fields_details = "";
			for(int i =0;i<fieldArray.length;i++)
			{
				if(unitArray[i].length()>0)
				{
					 temp = temp+ fieldArray[i]+","+unitArray[i]+"\\n";
				     fields_details = fields_details +fieldArray[i]+","+unitArray[i]+"<br>";
				}
				else
				{
					 temp =temp+ fieldArray[i]+"\\n";
					 fields_details = fields_details +fieldArray[i]+"<br>";
				}
			}
			fields =temp;
		}
	}

	//MVC to DB entity
	@Override
	public void fillDBfields() {
		// TODO Auto-generated method stub
		String [] fieldLines  = fields.split("\n");
		 fields = "";
		 unit = "";
		for(String item:fieldLines)
		{
			  int index = item.indexOf(",");
			  String fieldName ="";
			  String unitName = "";
			 
			  if(index>=0)
			  {
				  fieldName = item.substring(0,index);
				  unitName = item.substring(index+1);
			  }
			  else
			  {
				  fieldName = item;
			  }
			  fields = fields +fieldName +"|";
			  unit  = unit + unitName + "|";
		}
		if(fields.length()>0)
		{
			fields = fields.substring(0,fields.length()-1);
		}
		if(unit.length()>0)
		{
			unit = unit.substring(0,unit.length()-1);
		}
	}

	public int getSheet_def_Id() {
		return sheet_def_Id;
	}

	public void setSheet_def_Id(int sheet_def_Id) {
		this.sheet_def_Id = sheet_def_Id;
	}


	public String getFields() {
		return fields;
	}

	public void setFields(String fields) {
		this.fields = fields;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUnit() {
		return unit;
	}

	public void setUnit(String unit) {
		this.unit = unit;
	}

	public LabSheet() {
		super();
		displayName = "none";
		// TODO Auto-generated constructor stub
	}

	
	
}
